package com.jinjie.base.model;

import cn.hutool.http.HttpStatus;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1545366347409162701L;
    /**
     * 返回代码
     */
    private Integer code;
    /**
     * 成功标志
     */
    private boolean success;
    /**
     * 返回数据对象 data
     */
    private T result;
    /**
     * 返回处理消息
     */
    private String message;
    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = HttpStatus.HTTP_OK;
        this.success = true;
        return this;
    }

    public static <T> Result<T> OK() {
        return new Result<T>() .setSuccess(true) .setCode(HttpStatus.HTTP_OK) .setMessage("成功");

    }

    public static <T> Result OK(T data) {
        return new Result<T>() .setSuccess(true) .setResult(data) .setCode(HttpStatus.HTTP_OK) .setMessage("成功");
    }

    public static <T> Result<T> OK(T date, String message) {
        return new Result<T>() .setResult(date) .setMessage(message) .setCode(HttpStatus.HTTP_OK) .setSuccess(true);
    }

    public static <T> Result<T> error(String message, T data) {
        return new Result<T>().setResult(data).setCode(HttpStatus.HTTP_INTERNAL_ERROR).setMessage(message).setSuccess(false);
    }

    public static <T> Result<T> error(String message) {
        return new Result<T>().setMessage(message).setCode(HttpStatus.HTTP_INTERNAL_ERROR).setSuccess(false);
    }

    public static <T> Result<T> error(Integer code, String message) {
        return new Result<T>().setCode(code).setMessage(message).setSuccess(false);

    }
}
